﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Text;
using He.Common;
using He.Common.dto;

namespace He.Web.api
{
    /// <summary>
    /// 分类
    /// </summary>
    [RoutePrefix("api/category")]
    public class CategoryController : ApiController
    {
        /// <summary>
        /// 查询分类
        /// </summary>
        /// <param name="module">模块名称</param>
        /// <param name="parentId">父级分类Id</param>
        /// <param name="pageSize">每页显示数量</param>
        /// <param name="page">第几页</param>
        /// <returns></returns>
        [HttpGet, Route("{module}/list")]
        public Pagination<Model.category> GetCategorys(string module, int parentId = 0, int pageSize = 10, int page = 0)
        {
            BLL.category bll = new BLL.category();
            string condition = string.Format("channel_id in(select id from {0}channel where name='{1}') and parent_id={2}", SysKeys.TABLE_PREFIX, module, parentId);
            var count = bll.GetRecordCount(condition);
            var items = bll.DataTableToList(bll.GetPageList(pageSize, page, condition, "sort_id asc,id desc").Tables[0]);
            return new Pagination<Model.category>(count, items);
        }

        /// <summary>
        /// 获取所有子分类列表
        /// </summary>
        /// <param name="module">模块名称</param>
        /// <param name="pid">父级Id</param>
        /// <returns></returns>
        [HttpGet, Route("{module}/children/list")]
        public List<Model.category> GetChildrenList(string module, int pid)
        {
            BLL.category bll = new BLL.category();
            return bll.DataTableToList(bll.GetListByName(pid, module));
        }

    }
}
